'''
Created on 19/03/2009

@author: eh01
'''
from im.event.event import Event
from im.event.eventdispatcher import EventDispatcher
from im.event.eventhandler import BaseEventHandler
from im.event.eventqueue import EventQueue
from im.event.exception.eventhandlerexception import \
    EventHandlerExecutionException
import unittest

class EventManagerTest(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass

    def test_eventDispatcher_case1(self):
        """
        tests that all events are handled successfully
        """
                   
        eventHandlerA = EventHandlerA()
        eventHandlerB = EventHandlerB()
        eventHandlers = {'A':eventHandlerA, 'B':eventHandlerB}
        eventQueue = EventQueue()
        eventQueue.put(Event('A', '01'))
        eventQueue.put(Event('B', '02'))
        eventQueue.put(Event('A', '03'))
        eventQueue.put(Event('B', '04'))
        eventQueue.put(Event('A', '05'))
        eventQueue.put(Event('B', '06'))
        eventQueue.put(Event('A', '07'))
        eventQueue.put(Event('B', '08'))
        eventQueue.put(Event('B', '09'))
        eventQueue.put(Event('B', '10'))
        eventQueue.put(Event('B', '11'))
        eventQueue.put(Event('B', '12'))                
        eventDispatcher1 = EventDispatcher(eventQueue, eventHandlers)
        eventDispatcher2 = EventDispatcher(eventQueue, eventHandlers)
        eventDispatcher1.start()
        eventDispatcher2.start()
        eventQueue.put(Event('A', '13'))
        eventQueue.put(Event('B', '14'))
        eventQueue.put(Event('A', '15'))
        eventQueue.put(Event('B', '16'))
        eventQueue.put(Event('A', '17'))
        eventQueue.put(Event('B', '18'))
        eventQueue.put(Event('A', '19'))        
        eventQueue.put(Event('STOP', 'STOPPING'))
        eventQueue.put(Event('STOP', 'STOPPING'))
        
        while eventDispatcher1.isAlive() or eventDispatcher2.isAlive():
            print "waiting" 
        assert eventQueue.empty()

    def test_eventDispatcher_case2(self):
        """
        tests unable to find handler. A exception should be thrown and handled
        gracefully, and the dispatcher should continue onto the next event
        in the queue
        """
        eventHandlerA = EventHandlerA()
        eventHandlerB = EventHandlerB()
        eventHandlers = {'A':eventHandlerA, 'B':eventHandlerB}
        eventQueue = EventQueue()
        eventQueue.put(Event('A', '01'))
        eventQueue.put(Event('B', '02'))
        eventQueue.put(Event('C', '03'))
        eventQueue.put(Event('A', '04'))
        eventQueue.put(Event('B', '05'))        
        eventQueue.put(Event('STOP', 'STOPPING'))
        eventDispatcher1 = EventDispatcher(eventQueue, eventHandlers)
        eventDispatcher1.start()
        while eventDispatcher1.isAlive():
            print "waiting" 
        assert eventQueue.empty()                

    def test_eventDispatcher_case3(self):
        """
        tests exception raised in handler. The dispatcher should catch the 
        exception and should continue onto the next event in the queue
        """
        eventHandlerA = EventHandlerA()
        eventHandlerB = EventHandlerB()
        eventHandlerC = EventHandlerC()
        eventHandlers = {'A':eventHandlerA, 'B':eventHandlerB, 'C':eventHandlerC}
        eventQueue = EventQueue()
        eventQueue.put(Event('A', '01'))
        eventQueue.put(Event('B', '02'))
        eventQueue.put(Event('C', '03'))
        eventQueue.put(Event('A', '04'))
        eventQueue.put(Event('B', '05'))        
        eventQueue.put(Event('STOP', 'STOPPING'))
        eventDispatcher1 = EventDispatcher(eventQueue, eventHandlers)
        eventDispatcher1.start()
        while eventDispatcher1.isAlive():
            print "waiting" 
        assert eventQueue.empty()
                        
class EventHandlerA(BaseEventHandler):
    def do(self, data):
        print 'Hello From Handler A: data = ' + str(data)
class EventHandlerB(BaseEventHandler):
    def do(self, data):
        print 'Hello From Handler B: data = ' + str(data) 

class EventHandlerC(BaseEventHandler):
    def do(self, data):
        raise EventHandlerExecutionException("Execution Failure")             
                        
if __name__ == "__main__":

    unittest.main()